package cn.org.timespace.ons.dao.base.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.org.timespace.ons.action.base.OptionsModel;
import cn.org.timespace.ons.dao.TSBaseDAO;
import cn.org.timespace.ons.dao.base.OptionsDao;
import cn.org.timespace.ons.util.pager.Pager;
import cn.org.timespace.ons.vo.base.Options;



public class OptionsDaoImpl extends TSBaseDAO implements OptionsDao{

	@Override
	public List<Options> findByCondition(OptionsModel model) {
		// TODO Auto-generated method stub
		StringBuffer queryString = new StringBuffer("from Options a where 1=1");

		if (!StringUtils.isEmpty(model.getCode())) {
			queryString.append(" and a.code = '" + model.getCode()
					+ "'");
		}
		if (!StringUtils.isEmpty(model.getName())) {
			queryString.append(" and a.name like '%" + model.getName()
					+ "%'");
		}

		queryString.append(" order by a.theId desc");

		return getHibernateTemplate().find(queryString.toString());
	}
	public List<Object> findByCondition(OptionsModel model,Pager p) {
		log.debug("finding all Options instances");
		
		Session session = null;
		Query query = null;
		try {
			StringBuffer queryString = new StringBuffer("from Options a where 1=1 ");
			
			if (!StringUtils.isEmpty(model.getCode())) {
				queryString.append(" and a.code ='" + model.getCode()
						+ "'");
			}
			if (!StringUtils.isEmpty(model.getName())) {
				queryString.append(" and a.name like '%" + model.getName().trim()
						+ "%'");
			}
			
			
			
			queryString.append(" order by a.theId desc ");
			
			session = getSessionFactory().openSession();
			query = session.createQuery(queryString.toString());
			
			
			List<Object> resList = new ArrayList();
			resList.add(query.list().size());
			
			setQueryPager(query, p);
			resList.add(query.list());
			
			return resList;
			
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			closeSessionAndQuery(session, query);
		}
	}

	public void save(Options transientInstance) {
		log.debug("saving Options instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Options persistentInstance) {
		log.debug("deleting Options instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Options findById(java.lang.String theId) {
		log.debug("getting Options instance with id: " + theId);
		try {
			Options instance = (Options) getHibernateTemplate().get(
					"cn.org.timespace.ons.vo.base.Options", theId);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public void update(Options instance) {
		try {
			getHibernateTemplate().update(instance);
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}
	
	public Options doCheckOptionsNameIsExist(String name) {
		StringBuffer queryString = new StringBuffer("from Options a where a.name = '"+name+"'");
		List<Options> resList = getHibernateTemplate().find(queryString.toString());
		if(resList != null && resList.size() == 1){
			return resList.get(0);
		}
		return null;
	}
}
